In [1]:
import pandas as pd
In [4]:
# df = pd.read_csv('city.csv', sep=';', nrows=50)
df = pd.read_csv('city.csv', sep=';')
df
Out[4]:
ID Name CountryCode District Population
0 1 Kabul AFG Kabol 1780000
1 2 Qandahar AFG Qandahar 237500
2 3 Herat AFG Herat 186800
3 4 Mazar-e-Sharif AFG Balkh 127800
4 5 Amsterdam NLD Noord-Holland 731200
... ... ... ... ... ...
4074 4075 Khan Yunis PSE Khan Yunis 123175
4075 4076 Hebron PSE Hebron 119401
4076 4077 Jabaliya PSE North Gaza 113901
4077 4078 Nablus PSE Nablus 100231
4078 4079 Rafah PSE Rafah 92020

4079 rows × 5 columns

In [5]:
df['Location'] = df.Name + ', ' + df.CountryCode
In [6]:
df
Out[6]:
ID Name CountryCode District Population Location
0 1 Kabul AFG Kabol 1780000 Kabul, AFG
1 2 Qandahar AFG Qandahar 237500 Qandahar, AFG
2 3 Herat AFG Herat 186800 Herat, AFG
3 4 Mazar-e-Sharif AFG Balkh 127800 Mazar-e-Sharif, AFG
4 5 Amsterdam NLD Noord-Holland 731200 Amsterdam, NLD
... ... ... ... ... ... ...
4074 4075 Khan Yunis PSE Khan Yunis 123175 Khan Yunis, PSE
4075 4076 Hebron PSE Hebron 119401 Hebron, PSE
4076 4077 Jabaliya PSE North Gaza 113901 Jabaliya, PSE
4077 4078 Nablus PSE Nablus 100231 Nablus, PSE
4078 4079 Rafah PSE Rafah 92020 Rafah, PSE

4079 rows × 6 columns

In [11]:
df.rename(columns={'ID': 'Number'}, inplace=True)
In [13]:
df.to_csv('city2.csv', sep=';', index=False, columns=['Number', 'Location', 'Population'])

Excel Files

In [19]:
df2 = pd.read_excel('cities.xlsx', sheet_name=0)
df2
Out[19]:
ID Name CountryCode District Population
0 1 Kabul AFG Kabol 1780000
1 2 Qandahar AFG Qandahar 237500
2 3 Herat AFG Herat 186800
3 4 Mazar-e-Sharif AFG Balkh 127800
4 5 Amsterdam NLD Noord-Holland 731200
... ... ... ... ... ...
4074 4075 Khan Yunis PSE Khan Yunis 123175
4075 4076 Hebron PSE Hebron 119401
4076 4077 Jabaliya PSE North Gaza 113901
4077 4078 Nablus PSE Nablus 100231
4078 4079 Rafah PSE Rafah 92020

4079 rows × 5 columns

In [23]:
def convert_population(val):
    return '{:,}'.format(val)

def convert_population2(val):
    if val > 100000:
        return '{:,}'.format(val)
    return val
    
df2 = pd.read_excel('cities.xlsx', sheet_name=0, converters={'Population': convert_population})
df2
Out[23]:
ID Name CountryCode District Population
0 1 Kabul AFG Kabol 1,780,000
1 2 Qandahar AFG Qandahar 237,500
2 3 Herat AFG Herat 186,800
3 4 Mazar-e-Sharif AFG Balkh 127,800
4 5 Amsterdam NLD Noord-Holland 731,200
... ... ... ... ... ...
4074 4075 Khan Yunis PSE Khan Yunis 123,175
4075 4076 Hebron PSE Hebron 119,401
4076 4077 Jabaliya PSE North Gaza 113,901
4077 4078 Nablus PSE Nablus 100,231
4078 4079 Rafah PSE Rafah 92,020

4079 rows × 5 columns

In [27]:
df2.to_excel('cities2.xlsx', sheet_name='Format cities', index=False, startrow=3, startcol=1)
In [ ]:
 
In [28]:
df3 = pd.read_excel('cities.xlsx', sheet_name=1)
df3
Out[28]:
№ п/п Фамилия Имя Дата рождения
0 1 Иванов Иван 2000-01-31
1 2 Петров Петр 2001-11-10
In [ ]:
 
In [29]:
with pd.ExcelWriter('cities2.xlsx') as writer:
    df2.to_excel(writer, sheet_name='Format cities', index=False)
    df3.to_excel(writer, sheet_name='Names', index=False)
In [ ]:
 

Homework

  1. Считайте файл city.csv.
  2. Переименуйте все колонки в верхний регистр.
  3. Проверьте, в каких колонках есть отсутствующие значения.
  4. Удалите из датафрейма строки с отсутствующими значениями.
  5. Выберите в новый датафрейм строки с населением более 1 млн.
  6. Удалите столбец DISTRICT.
  7. Запишите датафрейм в файл test.xlsx без сохранения индексов.
In [ ]: